<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Models\Base\ServerList;
use App\Models\Base\ServerNotice;
use App\Service\RequestService;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;

class NoticeController extends Controller
{
    use ModelForm;
    private static $header;

    public function __construct()
    {
        self::$header = trans('game.gm_tool');
    }

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header(self::$header);
            $content->description(trans('admin.list'));

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     *
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header(self::$header);
            $content->description(trans('admin.edit'));

            $content->body($this->form()->edit($id));
            $notice = ServerNotice::find($id);
            if (2 == $notice->notice_type) {
                $this->editScript();
            }
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->header(self::$header);
            $content->description(trans('admin.new'));

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        ServerList::$soft = true;

        return Admin::grid(ServerNotice::class, function (Grid $grid) {
            $grid->model()->orderBy('id', 'desc');
            $grid->id('ID')->sortable();
            $grid->serverName()->name(trans('game.server_name'))->display(function ($server_name) {
                return $server_name ?? trans('game.server_all');
            });

            $grid->site_type(trans('game.type'))->display(function ($site_type) {
                switch ($site_type) {
                    case '0':
                        return trans('game.entertaining_diversions');
                        break;
                    case '1':
                        return trans('game.system_message');
                        break;
                    case '3':
                        return trans('game.dialog');
                        break;
                }
            });
            $grid->notice_type(trans('game.msg_type'))->display(function ($type) {
                return 1 == $type ? trans('game.ordinary') : trans('game.timing');
            });
            $grid->start_time(trans('game.start_time'));
            $grid->end_time(trans('game.end_time'));
            $grid->frequency(trans('game.frequency'))->display(function ($fre) {
                return $fre ? $fre.trans('game.min_ci') : trans('game.send_in_time');
            });
            $grid->content(trans('game.content'))->display(function ($content) {
                return str_limit($content, 15, '...');
            });
            $grid->created_at(trans('admin.created_at'));
            $grid->disableFilter();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(ServerNotice::class, function (Form $form) {
            $server = ServerList::all()->pluck('name', 'server_id');
            $serverList[0] = trans('game.server_all');
            foreach ($server as $key => $val) {
                $serverList[$key] = $val;
            }
            $form->display('id', 'ID');
            $form->select('server_id', trans('game.server_name'))->options($serverList)->rules('required|numeric');
            $form->radio('notice_type', trans('game.msg_type'))->options([1 => trans('game.ordinary'), 2 => trans('game.timing')])->default(1);
            $form->select('site_type', trans('game.type'))->options([0 => trans('game.entertaining_diversions'), 1 => trans('game.system_message'), 3 => trans('game.dialog')])->rules('required|numeric');
            $form->textarea('content', trans('game.content'))->rules('required|min:10|max:255');
            $form->datetimeRange('start_time', 'end_time', trans('admin.time'));

            $form->text('frequency', trans('game.frequency'))->default(0)->rules('numeric')->help(trans('game.frequency_help'));
            $form->display('created_at', trans('game.created_at'));
            $form->display('updated_at', trans('game.updated_at'));
            $this->script();

            $form->saving(function ($form) {
                if (!$this->sendGameNotice()) {
                    return back()->with(admin_toastr(trans('game.gs_communication_failed'), 'error'))->withInput();
                }
                $admin_name = \Auth::guard('admin')->user()->name;
                $client_ip = request()->getClientIp();
                $form->model()->admin_user = $admin_name;
                $form->model()->client_ip = $client_ip;
            });
        });
    }

    private function sendGameNotice()
    {
        $post = request()->post();
        $option = $post['site_type'].';'.$post['content'].'; ';

        return RequestService::sendNotice($post['server_id'], $option);
    }

    private function script()
    {
        $str = <<<EOF
            $(".start_time").parents('.form-group').hide();
            $(".frequency").parents('.form-group').hide();
        $(".radio-inline").on('click',function(){
            var val = $(this).find('input').val();
            if(val == 1){
                $(".start_time").parents('.form-group').hide();
                $(".frequency").parents('.form-group').hide();
            }else {
                $(".start_time").parents('.form-group').show();
                $(".frequency").parents('.form-group').show();
            }
        });
        var radio_val = $(".radio-inline").find('input').val();
        console.log(radio_val);
EOF;
        Admin::script($str);
    }

    private function editScript()
    {
        $str = <<<EOF
            $(".start_time").parents('.form-group').show();
            $(".frequency").parents('.form-group').show();
EOF;
        Admin::script($str);
    }
}
